package com.newpage.module_gather.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.google.gson.reflect.TypeToken;
import com.newpage.module_gather.R;
import com.newpage.module_gather.R2;
import com.newpage.module_gather.adapter.CommonPopAdapter;
import com.newpage.module_gather.contract.GatherMainCon;
import com.newpage.module_gather.fragment.QuestionManagerFragment;
import com.newpage.module_gather.fragment.QuestionRevisionFragment;
import com.newpage.module_gather.presenter.GatherMainPre;
import com.wetao.lib_common.ConstantArouter;
import com.wetao.lib_common.Constants;
import com.wetao.lib_common.base.BaseActivity;
import com.wetao.lib_common.base.BasePresenter;
import com.wetao.lib_common.base.BaseViewImp;
import com.wetao.lib_common.bean.TabEntity;
import com.wetao.lib_common.bean.response.ExplainBean;
import com.wetao.lib_common.bean.response.SubjectInfo;
import com.wetao.lib_common.tsdBlePen.TsdService;
import com.wetao.lib_common.utils.BaseEvent;
import com.wetao.lib_common.utils.ComUtil;
import com.wetao.lib_common.utils.GsonUtil;
import com.wetao.lib_common.utils.JsonUtils;
import com.wetao.lib_common.utils.ToastUtil;
import com.wetao.lib_common.view.CustomPopWindow;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


@Route(path = ConstantArouter.PATH_GATHER_MAINACTIVITY)
public class GatherMainActivity extends BaseActivity<GatherMainCon.View, GatherMainPre> implements GatherMainCon.View,View.OnClickListener {


    @Autowired
    String subjectInfo;

    private ImageView iv_back;
    private ViewPager viewPager;
    private CommonTabLayout ctl_tab;
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    private FragmentManager fragmentManager;//声明fragment管理
    private List<Fragment> fragmentList = new ArrayList<>();
    private String[] mTitles = {"错题管理","错题订正"};

    private QuestionManagerFragment questionManagerFragment;
    private QuestionRevisionFragment questionRevisionFragment;


    @Override
    public int getLayoutId() {
        return R.layout.gather_activity_main;
    }

    @Override
    public GatherMainPre createPresenter() {
        return new GatherMainPre(this);
    }

    @Override
    public GatherMainCon.View createView() {
        return this;
    }

    @Override
    public void init() {
        if (!EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().register(this);
        }
        initView();
        initData();
    }



    private void initView() {

        viewPager = findViewById(R.id.vp);
        ctl_tab = findViewById(R.id.tl_tab);
        iv_back = findViewById(R.id.iv_gather_main_back);

        iv_back.setOnClickListener(this);


    }


    /**
     * 初始化数据
     */
    private void initData(){

        questionManagerFragment = new QuestionManagerFragment();
        questionRevisionFragment = new QuestionRevisionFragment();
        fragmentList.add(questionManagerFragment);
        fragmentList.add(questionRevisionFragment);

//        Bundle bundle = new Bundle();
//        bundle.putString("subjectInfo",subjectInfo);
//        questionManagerFragment.setArguments(bundle);
//        questionRevisionFragment.setArguments(bundle);
        fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().commit();


        FragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(fragmentManager);
        viewPager.setAdapter(pagerAdapter);
        viewPager.setOffscreenPageLimit(fragmentList.size());
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                ctl_tab.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i], 0, 0));
        }
        ctl_tab.setTabData(mTabEntities);


        ctl_tab.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                viewPager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });

    }


    @Override
    public boolean getIsBindTsdService() {
        return false;
    }

    @Override
    public TsdService.OnDataReceiveListener getTsdServiceDataReceiveListener() {
        return null;
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_gather_main_back){
            finish();
        }
    }



    class MyFragmentPagerAdapter extends FragmentPagerAdapter {
        public MyFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
        }
        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return super.getPageTitle(position);
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
    public void onEventReceived(BaseEvent event) {

    }

}